import random

HS300_2009 = [
    '600685.XSHG', '000629.XSHE', '601601.XSHG', '600653.XSHG', '600033.XSHG', '600108.XSHG', '600352.XSHG',
    '600548.XSHG', '000401.XSHE', '600104.XSHG', '601991.XSHG', '000768.XSHE', '000937.XSHE', '000807.XSHE',
    '600030.XSHG', '601727.XSHG', '000538.XSHE', '600350.XSHG', '601958.XSHG', '600690.XSHG', '000927.XSHE',
    '600004.XSHG', '600497.XSHG', '600585.XSHG', '000960.XSHE', '600320.XSHG', '000031.XSHE', '600125.XSHG',
    '000951.XSHE', '000878.XSHE', '600970.XSHG', '600588.XSHG', '600426.XSHG', '601318.XSHG', '600251.XSHG',
    '600028.XSHG', '600085.XSHG', '600096.XSHG', '600550.XSHG', '000623.XSHE', '600357.XSHG', '600547.XSHG',
    '600635.XSHG', '600782.XSHG', '600717.XSHG', '601919.XSHG', '601857.XSHG', '002202.XSHE', '600804.XSHG',
    '601699.XSHG', '600036.XSHG', '600048.XSHG', '601328.XSHG', '600674.XSHG', '000825.XSHE', '000895.XSHE',
    '601998.XSHG', '000680.XSHE', '600837.XSHG', '600795.XSHG', '600616.XSHG', '000968.XSHE', '600188.XSHG',
    '000728.XSHE', '601186.XSHG', '000089.XSHE', '601009.XSHG', '600508.XSHG', '600811.XSHG', '000425.XSHE',
    '000717.XSHE', '000917.XSHE', '600111.XSHG', '000822.XSHE', '000069.XSHE', '600611.XSHG', '002038.XSHE',
    '000959.XSHE', '600016.XSHG', '600153.XSHG', '600331.XSHG', '000652.XSHE', '000488.XSHE', '600395.XSHG',
    '600694.XSHG', '600309.XSHG', '600649.XSHG', '600595.XSHG', '600737.XSHG', '000562.XSHE', '601333.XSHG',
    '600029.XSHG', '600779.XSHG', '600308.XSHG', '601166.XSHG', '600376.XSHG', '000898.XSHE', '000001.XSHE',
    '000793.XSHE', '600143.XSHG', '601600.XSHG', '600256.XSHG', '600839.XSHG', '002128.XSHE', '000932.XSHE',
    '600196.XSHG', '600569.XSHG', '000729.XSHE', '000338.XSHE', '600383.XSHG', '002028.XSHE', '600110.XSHG',
    '601001.XSHG', '601808.XSHG', '000046.XSHE', '600688.XSHG', '600664.XSHG', '600489.XSHG', '600597.XSHG',
    '600643.XSHG', '000685.XSHE', '600362.XSHG', '000725.XSHE', '600017.XSHG', '600221.XSHG', '600549.XSHG',
    '600026.XSHG', '600307.XSHG', '600741.XSHG', '000800.XSHE', '600031.XSHG', '000625.XSHE', '600886.XSHG',
    '600266.XSHG', '600428.XSHG', '000423.XSHE', '600177.XSHG', '000858.XSHE', '600118.XSHG', '000686.XSHE',
    '600770.XSHG', '600516.XSHG', '600663.XSHG', '600001.XSHG', '601866.XSHG', '600874.XSHG', '000983.XSHE',
    '600812.XSHG', '000667.XSHE', '000778.XSHE', '000002.XSHE', '601939.XSHG', '000157.XSHE', '600600.XSHG',
    '000933.XSHE', '600089.XSHG', '600835.XSHG', '002269.XSHE', '600269.XSHG', '600900.XSHG', '601169.XSHG',
    '600316.XSHG', '002122.XSHE', '600018.XSHG', '600655.XSHG', '600006.XSHG', '600019.XSHG', '600169.XSHG',
    '600008.XSHG', '600418.XSHG', '600176.XSHG', '600380.XSHG', '002142.XSHE', '002001.XSHE', '600638.XSHG',
    '600881.XSHG', '600000.XSHG', '600875.XSHG', '002244.XSHE', '600102.XSHG', '000651.XSHE', '601390.XSHG',
    '600236.XSHG', '000900.XSHE', '000021.XSHE', '600068.XSHG', '600601.XSHG', '000568.XSHE', '600158.XSHG',
    '600027.XSHG', '600015.XSHG', '600219.XSHG', '600037.XSHG', '002024.XSHE', '600528.XSHG', '601988.XSHG',
    '601899.XSHG', '601005.XSHG', '600123.XSHG', '600005.XSHG', '000559.XSHE', '600820.XSHG', '601398.XSHG',
    '000718.XSHE', '600583.XSHG', '600675.XSHG', '600100.XSHG', '600718.XSHG', '600010.XSHG', '601766.XSHG',
    '000528.XSHE', '600432.XSHG', '000009.XSHE', '000543.XSHE', '000100.XSHE', '600325.XSHG', '600660.XSHG',
    '600879.XSHG', '600270.XSHG', '601666.XSHG', '002242.XSHE', '000027.XSHE', '600050.XSHG', '600022.XSHG',
    '600598.XSHG', '600276.XSHG', '000709.XSHE', '601628.XSHG', '600748.XSHG', '600518.XSHG', '600642.XSHG',
    '600210.XSHG', '000969.XSHE', '000612.XSHE', '000039.XSHE', '000783.XSHE', '000063.XSHE', '002155.XSHE',
    '600596.XSHG', '002194.XSHE', '000061.XSHE', '600087.XSHG', '600816.XSHG', '600117.XSHG', '600832.XSHG',
    '600183.XSHG', '600058.XSHG', '600170.XSHG', '600216.XSHG', '600151.XSHG', '601898.XSHG', '600809.XSHG',
    '600415.XSHG', '000912.XSHE', '000758.XSHE', '000792.XSHE', '600631.XSHG', '000422.XSHE', '600895.XSHG',
    '600132.XSHG', '000012.XSHE', '600456.XSHG', '000060.XSHE', '600739.XSHG', '000059.XSHE', '600109.XSHG',
    '600282.XSHG', '600011.XSHG', '601872.XSHG', '000690.XSHE', '000301.XSHE', '600519.XSHG', '600582.XSHG',
    '000999.XSHE', '601088.XSHG', '600009.XSHG', '600271.XSHG', '600808.XSHG', '000024.XSHE', '000839.XSHE',
    '600299.XSHG', '601111.XSHG', '600150.XSHG', '600997.XSHG', '601588.XSHG', '600500.XSHG', '601918.XSHG',
    '000527.XSHE', '600220.XSHG', '600066.XSHG', '600639.XSHG', '601168.XSHG', '000876.XSHE', '000630.XSHE',
    '601006.XSHG', '000897.XSHE', '600859.XSHG', '600208.XSHG', '600348.XSHG', '000402.XSHE'
]

CONFIG = {
    "base": {
        "start_date": "20090901",
        "end_date": "20190901",
        "accounts": {
            "STOCK": 10e8
        }
    },
    "extra": {
        "log_level": "error"
    },
    "mod": {
        "sys_analyser": {
            "enabled": "false"
        }
    }
}


def init(context):
    random.seed = 1
    context.counter = 0


def handle_bar(context, _):
    context.counter += 1
    if context.counter == 1:
        for order_book_id in HS300_2009:
            order_shares(order_book_id, 200)
    else:
        for _ in range(5):
            order_book_id = random.choice(HS300_2009)
            order_shares(order_book_id, 200)
        for _ in range(5):
            order_book_id = random.choice(context.portfolio.positions.keys())
            order_shares(order_book_id, -200)


if __name__ == "__main__":
    from rqalpha import run_func
    run_func(init=init, handle_bar=handle_bar, config=CONFIG)

    """
    RQalpha 3.4.0
    
    77.30s user 1.85s system 99% cpu 1:19.94 total
     
    iMac (Retina 5K, 27-inch, 2017)
    4.2 GHz Intel Core i7
    40 GB 2400 MHz DDR4
    """
